import { defineEventHandler, getQuery } from 'h3';

export default defineEventHandler(async (event) => {
  const query = getQuery(event);

  // SaaS应用数据
  const applications = [
    {
      id: '1',
      name: '智慧酒店管理系统',
      description: '基于SaaS的酒店管理解决方案',
      industryType: 'hotel',
      status: 'running',
      deployment: {
        environment: 'prod',
        region: 'cn-hangzhou',
        domain: 'hotel.example.com',
        version: '1.0.0',
        deployedAt: '2024-01-15T10:30:00Z',
      },
      usage: {
        users: 150,
        storage: 1024,
        bandwidth: 512,
        lastActive: '2024-01-15T16:20:00Z',
      },
      createdAt: '2024-01-10T09:00:00Z',
      createdBy: 'admin',
    },
    {
      id: '2',
      name: '宠物店管理系统',
      description: '宠物店业务管理平台',
      industryType: 'pet',
      status: 'building',
      deployment: {
        environment: 'dev',
        region: 'cn-beijing',
        domain: '',
        version: '0.9.0',
        deployedAt: '',
      },
      usage: {
        users: 0,
        storage: 0,
        bandwidth: 0,
        lastActive: '',
      },
      createdAt: '2024-01-12T14:30:00Z',
      createdBy: 'admin',
    },
    {
      id: '3',
      name: '温泉度假村管理系统',
      description: '温泉度假村综合管理平台',
      industryType: 'spa',
      status: 'running',
      deployment: {
        environment: 'prod',
        region: 'cn-shanghai',
        domain: 'spa.example.com',
        version: '1.2.0',
        deployedAt: '2024-01-08T14:20:00Z',
      },
      usage: {
        users: 80,
        storage: 512,
        bandwidth: 256,
        lastActive: '2024-01-15T18:30:00Z',
      },
      createdAt: '2024-01-05T11:15:00Z',
      createdBy: 'admin',
    },
    {
      id: '4',
      name: '景区票务管理系统',
      description: '景区门票销售和管理系统',
      industryType: 'scenic',
      status: 'deploying',
      deployment: {
        environment: 'staging',
        region: 'cn-guangzhou',
        domain: 'ticket-staging.example.com',
        version: '1.1.0',
        deployedAt: '2024-01-14T16:45:00Z',
      },
      usage: {
        users: 25,
        storage: 128,
        bandwidth: 64,
        lastActive: '2024-01-15T12:10:00Z',
      },
      createdAt: '2024-01-13T09:30:00Z',
      createdBy: 'admin',
    },
    {
      id: '5',
      name: '餐饮连锁管理系统',
      description: '餐饮连锁店统一管理平台',
      industryType: 'restaurant',
      status: 'running',
      deployment: {
        environment: 'prod',
        region: 'cn-shenzhen',
        domain: 'restaurant.example.com',
        version: '1.3.0',
        deployedAt: '2024-01-06T10:15:00Z',
      },
      usage: {
        users: 200,
        storage: 2048,
        bandwidth: 1024,
        lastActive: '2024-01-15T19:45:00Z',
      },
      createdAt: '2024-01-03T15:20:00Z',
      createdBy: 'admin',
    },
  ];

  // 过滤逻辑
  let filteredApplications = applications;

  if (query.keyword) {
    const keyword = query.keyword as string;
    filteredApplications = filteredApplications.filter(
      (app) => app.name.includes(keyword) || app.description.includes(keyword),
    );
  }

  if (query.industryType) {
    filteredApplications = filteredApplications.filter(
      (app) => app.industryType === query.industryType,
    );
  }

  if (query.status) {
    filteredApplications = filteredApplications.filter(
      (app) => app.status === query.status,
    );
  }

  // 分页
  const page = parseInt(query.page as string) || 1;
  const pageSize = parseInt(query.pageSize as string) || 10;
  const start = (page - 1) * pageSize;
  const end = start + pageSize;
  const items = filteredApplications.slice(start, end);

  return {
    code: 0,
    data: {
      items,
      total: filteredApplications.length,
      page,
      pageSize,
    },
    msg: '获取SaaS应用列表成功',
  };
});

